/*
 * @Author: jasper
 * @Date: 2021-09-26 13:32:21
 * @LastEditors: baron
 * @LastEditTime: 2025-04-17 15:52:44
 * @Description:
 */
import Request from 'luch-request';

const pro = false; //为测试环境
// const pro = true; //为生产环境

const proUrl = 'https://apollo.jlbry.cn:9443';

const testUrl = 'http://52.130.57.86:8081'; // 测试服务器
// const testUrl = 'http://192.168.0.24:443/api'; // ibis测试

const apiURL = pro ? proUrl : testUrl;
const http = new Request();
http.setConfig(config => {
    config.baseURL = apiURL;
    config.dataType = 'json';
    config.timeout = 20000;
    return config;
});

http.interceptors.request.use(
    config => {
        try {
            // 'Bearer ' + 
            config.header['Authorization'] = uni.getStorageSync('token');
            const { showLoading } = config.custom;
            showLoading && uni.showLoading({ title: '正在加载中~', mask: true });

        } catch (error) {
            console.log(e, '-request--fail');
            config.header['Authorization'] = uni.getStorageSync('token');
        }
        return config;
    },
    config => {
        return Promise.reject(config);
    }
);

http.interceptors.response.use(
    response => {
        // 埋点接口不处理
        if (response.config.url !== '/action/customerOperate') {
           
            uni.hideLoading();
            if (response.data.code === 200 || response.data.code === 0) {
                return response.data;
            } else if (response.data.code === 400) {
                uni.showModal({
                    title: response.data?.msg,
                    showCancel: false,
                });
                return Promise.reject(response);
            } else if (response.data.code === 401) {
                uni.removeStorageSync('token');
                uni.showModal({
                    title: response.data?.msg,
                    showCancel: false,
                });
                return Promise.reject(response);
            } else if (response.data.code === 500) {
                uni.showModal({
                    title: response.data?.msg,
                    showCancel: false,
                });
                return Promise.reject(response);
            }
            return Promise.reject(response);
        }
    },
    response => {
        if (response.statusCode === 401) {
            uni.removeStorageSync('token');
            uni.showModal({
                title: '没有权限，请先登录',
            });
        } else {
            uni.showToast({
                title: response.data?.msg || '服务器好像出了点问题~',
                icon: 'none',
                duration: 1500
            });
        }
        uni.hideLoading();
        uni.getNetworkType({
            success: function (res) {
                if (res.networkType === 'none') {
                    uni.showModal({
                        title: '您当前的网络不可用，请检查您的网络设置~~',
                        showCancel: false
                    });
                }
            }
        });
        return Promise.reject(response);
    }
);

/**
 * @Description GET请求
 * @param url 请求地址
 * @param params 请求参数  默认 {}
 * @param showLoading 是否显示全局Loading动画
 */
export const get = (url, params = {}) => {
    const {
        showLoading = true
    } = params;
    console.log(url, '--参数--', params);
    return http.get(url, {
        params,
        custom: {
            showLoading
        }
    });
};

/**
 * @Description POST请求
 * @param url 请求地址
 * @param data 请求参数 默认 {}
 * @param showLoading 是否显示全局Loading动画
 */
export const post = (url, params = {}) => {
    const {
        showLoading = true
    } = params;
    console.log(url, '--参数--', params);
    return http.post(url, params, {
        custom: {
            showLoading
        }
    });
};

export { apiURL as baseURL };
export { pro };
